package com.handinfo.game;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.Actor;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.Resource;
import com.handinfo.uimenu.window.UIMain;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;

public abstract class AbstractGameActor extends Actor
{
	public AbstractGameActor()
	{
		super();
		m_hp = m_hpMax = 1;
		m_mp = m_mpMax = 1;
	}

	public AbstractGameActor(Resource resource)
	{
		super(resource);
		m_hp = m_hpMax = 1;
		m_mp = m_mpMax = 1;
	}

	// 类型
	public static final int ACTOR_TYPE_ROLE = 0;// 角色
	public static final int ACTOR_TYPE_NPC = 1;// NPC
	public static final int ACTOR_TYPE_MONSTER = 2;// 怪物
	public static final int ACTOR_TYPE_GATHERING = 3; // 可采集的物
	public static final int ACTOR_TYPE_GATE = 4; // 传送门
	public static final int ACTOR_TYPE_MAPPOINT = 5; // 传送点
	public static final int ACTOR_TYPE_RELIVE = 6; // 复活点
	public static final int ACTOR_TYPE_ITEM = 7; // 掉落道具装备
	public static final int ACTOR_TYPE_FAZHEN = 8;//法阵

	// 状态
	public static final int ACTOR_STATUS_ROLE_FREE = 8;// 玩家自由状态
	public static final int ACTOR_STATUS_ROLE_COMBAT = 16;// 玩家战斗状态
	public static final int ACTOR_STATUS_ROLE_DEAD = 32;// 玩家死亡状态

	public static final int ACTOR_STATUS_NPC_FREE = 10;// NPC和怪物自由状态
	public static final int ACTOR_STATUS_NPC_COMBAT = 13;// NPC和怪物战斗状态
	public static final int ACTOR_STATUS_NPC_DEAD = 11;// NPC和怪物死亡状态

	// 行为
	public static final int ACTOR_ACTION_NONE = 0; // 无行为
	public static final int ACTOR_ACTION_RANDOM_MOVE = 1; // 随机移动 无法移动 可以施法
	public static final int ACTOR_ACTION_FORBIT_MOVE = 2; // 禁止移动 无法移动 可以施法
	public static final int ACTOR_ACTION_SCRIPT_MOVE = 4; // 脚本移动 无法移动 可以施法
	public static final int ACTOR_ACTION_FORBIT_SKILL = 8; // 禁止施法 可以移动 无法释放
	public static final int ACTOR_ACTION_FORBIT_ATTACK_SKILL = 16; // 定身 无法移动 无法施法

	// 方向
	public static final int DIRECT_E = 0;// 东
	public static final int DIRECT_S = 1;// 南
	public static final int DIRECT_W = 2;// 西
	public static final int DIRECT_N = 3;// 北
	public static final int DIRECT_NUM = 4;

	// 动作
	public static final int ACTION_STAND = 0;
	public static final int ACTION_MOVE = 1;

	// 阵营
	public static final int FACTION_ANY = 0;//任意
	public static final int FACTION_1 = 1; // 阵营1
	public static final int FACTION_2 = 2; // 阵营2
	public static final int FACTION_FRIEND = 3; // 对所有都友善
	public static final int FACTION_ENEMY = 4; // 对所有都敌对
	public static final int FACTION_3 = 5;//阵营3

	public static final String[] FACTION_WORD = { "任意", "昆", "泰", "中立", "敌对", "洞" };

	/**
	 * 移动
	 * 
	 * @param direct
	 */
	public void move(int direct)
	{
		switch (direct) {
			case DIRECT_E:
				m_flipX = false;
				setAction(ACTION_MOVE * DIRECT_NUM + direct, m_velocity, 0, true, false);
				break;
			case DIRECT_S:
				if (m_direct == DIRECT_W) {
					m_flipX = true;
				}
				setAction(ACTION_MOVE * DIRECT_NUM + direct, 0, m_velocity, true, false);
				break;
			case DIRECT_W:
				m_flipX = false;
				setAction(ACTION_MOVE * DIRECT_NUM + direct, -m_velocity, 0, true, false);
				break;
			case DIRECT_N:
				if (m_direct == DIRECT_W) {
					m_flipX = true;
				}
				setAction(ACTION_MOVE * DIRECT_NUM + direct, 0, -m_velocity, true, false);
				break;
		}
	}

	/**
	 * 移动
	 * 
	 * @param direct
	 */
	public void move(int direct, int velocity)
	{
		switch (direct) {
			case DIRECT_E:
				m_flipX = false;
				setAction(ACTION_MOVE * DIRECT_NUM + direct, velocity, 0, true, false);
				break;
			case DIRECT_S:
				if (m_direct == DIRECT_W) {
					m_flipX = true;
				}
				setAction(ACTION_MOVE * DIRECT_NUM + direct, 0, velocity, true, false);
				break;
			case DIRECT_W:
				m_flipX = false;
				setAction(ACTION_MOVE * DIRECT_NUM + direct, velocity, 0, true, false);
				break;
			case DIRECT_N:
				if (m_direct == DIRECT_W) {
					m_flipX = true;
				}
				setAction(ACTION_MOVE * DIRECT_NUM + direct, 0, velocity, true, false);
				break;
		}
	}

	/**
	 * 站立
	 */
	public void stay(int direct)
	{
		setAction(ACTION_STAND * DIRECT_NUM + direct, 0, 0, true, false);
	}

	/**
	 * 设定元件动作
	 * 
	 * @param actionIDNext
	 * @param mustChange
	 */
	public void setAction(int actionIDNext, boolean mustChange)
	{
		try {
			if (m_anim == null || m_anim.m_actions == null) {
				if (Tools.DEBUG) {
					System.err.println("AbstratGameActor ERROR: m_anim为空" + ";Actor.name=" + m_name);
				}
				return;
			}
			m_directBefore = m_direct;
			if (!mustChange) {
				if (m_actionIDNow == actionIDNext) {
					return;
				}
			}
			if (m_actionIDNow != actionIDNext) {
				m_actionIDBefore = m_actionIDNow;
			}

			m_actionIDNow = actionIDNext;
			int direct = m_actionIDNow % 4;
			m_direct = (byte) direct;

			m_frameId = 0;
			if (m_actionIDNow < 0) {
				if (Tools.DEBUG) {
					System.err.println("ERROR: actionIDNow < 0 " + m_actionIDNow);
				}
			}
			else if (m_anim != null && m_anim.m_actions != null && m_actionIDNow < m_anim.m_actions.length) {
				if (m_anim.m_actions[m_actionIDNow].length != 0) {
					m_frameIndex = m_anim.m_actions[m_actionIDNow][0] & 0x03FF;
				}
			}
			else if (m_anim != null) {
				m_frameIndex = m_anim.m_actions[m_actionIDNow
						% m_anim.m_actions.length][0] & 0x03FF;
			}
			m_frameDuration = 0;
			m_actionOver = false;
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	// ///////////////////////////////////////////////////////////
	// // 基础属性
	public String m_name = null;// 该角色的名字
	public int m_lv = 0;// 该角色的等级
	public byte m_faction = 0;// 该角色从属于哪个阵营
	public boolean m_combating = false; // 是否在战斗中
	public String m_location = null;// 角色最后下线的场景名称

	public static final String[] ROLE_FACTION_LIST = { "无 ", "纯阳", "灵隐", "中立", "敌对", "天机" };

	// REALM_ALL = 0, //任意
	// KUNLUN = 1, //昆仑
	// TAISHAN = 2, //泰山
	// NEUTRAL = 3, //中立
	// WARRING = 4, //敌对
	// WANGSICHENG = 5, //枉死城

	public int m_thumbnail_color = 0;// 该角色在缩略图中的颜色
	public int m_thumbnail_x = 0;// 该角色在缩略图中的x
	public int m_thumbnail_y = 0;// 该角色在缩略图中的y

	public int m_hp, m_hpMax;
	public int m_mp, m_mpMax;

	public byte m_type = 0;// NPC=0, 怪=1, 角色=2
	public byte m_direct = 0;
	public byte m_directBefore = 0;

	public AbstractGameActor m_targetActor; // 目标actor

	/** 角色状态 */
	public int m_status = 0; // 状态
	/** 角色行为 */
	public int m_action = 0;// 行为

	public int m_head_state = 0;// NPC头上状态
	public byte m_function_icon = 0;//NPC功能图标
	public String m_head_tips = null;//NPC头上状态提示

	public boolean m_needSendPath = false; // 是否需要发送行进拐点
	public ArrayList m_pathPoint = new ArrayList(); // 行进拐点

	/**
	 * 对于目标的伤害记录
	 */
	public Vector m_targetDamage = new Vector();
	/**
	 * 当前战斗技能 （对于玩家角色而言，当前技能为空时，才能施放下个技能）
	 */
	public CombatSkill m_curCombatSkill;
	public GameEffect m_castSrcEffect; // 施法者光效（一般用于需要持续显现的吟唱或者通道法术）
	public GameEffect m_castTgtEffect; // 施法目标光效（一般用于需要持续显现的吟唱或者通道法术）

	// // 基础属性
	// ///////////////////////////////////////////////////////////

	public static final int SENDPATH_INTERVAL = 10;

	public void logic()
	{
		super.logic();
		if (m_needSendPath) {
			if (m_pathPoint == null) {
				m_pathPoint = new ArrayList();
			}
			// 定时发送移动信息
			if (m_scene.m_frameCount % SENDPATH_INTERVAL == 0) {
				int info = m_posX | (m_posY << 16);
				m_pathPoint.addElement(new Integer(info));
				GameManager.getInstance().m_game_message.sendMove(m_type, m_pathPoint);
				m_pathPoint.removeAllElements();
			}
			if (m_direct != m_directBefore) {
				int info = m_posX | (m_posY << 16);
				m_pathPoint.addElement(new Integer(info));
			}
		}
		updateMove();
	}

	/**
	 * 临时发送一次同屏移动
	 */
	public void sendOnecMove()
	{
		int info = m_posX | (m_posY << 16);
		m_pathPoint.addElement(new Integer(info));
		GameManager.getInstance().m_game_message.sendMove(m_type, m_pathPoint);
		m_pathPoint.removeAllElements();
	}

	public void updatePos()
	{
		// AbstractGameActor对象在非动画情况下才更新位置信息
		if (m_scene.m_script == null || m_scene.m_script.m_trailerEnd) {
			super.updatePos();
		}
	}

	/**
	 * 转向目标
	 * 
	 * @param target
	 */
	public void changeDirectToTarget(AbstractGameActor target)
	{
		if (target == null)
			return;
		if (target.m_posY < m_posY && target.m_posX < m_posX) {
			m_flipX = true;
			m_direct = DIRECT_N;
		}
		else if (target.m_posX < m_posX) {
			m_flipX = false;
			m_direct = DIRECT_W;
		}
		else if (target.m_posY < m_posY) {
			m_flipX = false;
			m_direct = DIRECT_N;
		}
		else {
			m_flipX = false;
			m_direct = DIRECT_E;
		}
	}

	public boolean checkTargetFriend(AbstractGameActor actor)
	{
		// Tools.debugPrintln("me : " + m_faction + " : " + actor.m_faction);
		if (actor.m_faction == m_faction) {
			return true;
		}
		if (m_faction == FACTION_ENEMY || actor.m_faction == FACTION_ENEMY) {
			return false;
		}
		if (m_faction == FACTION_ANY || actor.m_faction == FACTION_ANY) {
			return true;
		}
		if (m_faction == FACTION_FRIEND || actor.m_faction == FACTION_FRIEND) {
			return true;
		}
		else {
			return false;
		}
	}

	public boolean checkTargetFactionRole(AbstractGameActor actor)
	{
		if (actor.m_faction == m_faction && actor.m_type == AbstractGameActor.ACTOR_TYPE_ROLE) {
			return true;
		}
		return false;
	}

	public final int DAMAGE_NUM_OFFSETY = 30;// 伤害显示位移Y

	/**
	 * 处理目标伤害，受伤动作以及光效
	 */
	public boolean dealTargetDamage(boolean dealTgtAct)
	{
		if (m_targetDamage.size() == 0)
			return false;
		GameScene scene = (GameScene) m_scene;
		AbstractGameActor controlleActor = ((GameScene) m_scene).m_controlledActor;
		for (int i = 0; i < m_targetDamage.size(); i++) {
			long[] info = (long[]) m_targetDamage.elementAt(i);
			byte type = (byte) info[0];
			long id = info[1];
			int hValue = (int) info[2];
			boolean crit = (info[3] & 0x01) == 1; // 是否暴击
			boolean miss = (info[3] & 0x02) == 0; // 是否命中
			boolean interrupt = (info[3] & 0x04) == 1; // 是否打断
			boolean absorb = (info[3] & 0x08) == 1; // 是否吸收
			// 获得被攻击对象
			AbstractGameActor r = (AbstractGameActor) scene.getActorById(type, id);
			// 如果被攻击对象异常则跳过
			if (r == null || r.m_status == ACTOR_STATUS_ROLE_DEAD) {
				m_targetDamage.removeAllElements();
				continue;
			}
			// 攻击命中
			if (!miss) {
				if (dealTgtAct && hValue < 0) { // 需要处理目标的动作以及光效
					// 根据被攻击对象类型以及攻击结果决定受伤动作
					switch (type) {
						case AbstractGameActor.ACTOR_TYPE_NPC:
						case AbstractGameActor.ACTOR_TYPE_MONSTER: {
							// 暴击 + 受伤或者打断
							if (crit && r.canBeInterrupt(interrupt)) {
								r.setAction(ActorNPC.ACTION_HURT2 * ActorNPC.DIRECT_NUM + r.m_direct, false, true);
							}
							// 普通 + 受伤或者打断
							else if (!crit && r.canBeInterrupt(interrupt)) {
								r.setAction(ActorNPC.ACTION_HURT1 * ActorNPC.DIRECT_NUM + r.m_direct, false, true);
							}
							break;
						}
						case AbstractGameActor.ACTOR_TYPE_ROLE: {
							// 如果打断，则将目标stopcasting
							if (interrupt) {
								((ActorRole) r).m_isCasting = false;
								((ActorRole) r).stopCasting();
								((ActorRole) r).stopCastingDescription();
							}
							// 暴击 + 受伤
							if (crit && r.canBeInterrupt(interrupt)) {
								r.setAction(ActorRole.ACTION_HURT2 * ActorRole.DIRECT_NUM + r.m_direct, false, true);
							}
							// 普通 + 受伤
							else if (!crit && r.canBeInterrupt(interrupt)) {
								r.setAction(ActorRole.ACTION_HURT1 * ActorRole.DIRECT_NUM + r.m_direct, false, true);
							}
							break;
						}
					}
					scene.addEffect(41, r.m_posX + Tools.random(-5, 5), r.m_posY + Tools.random(-5, 5));
				}
				// 只有自己是施法方或者施法目标时才显示数值相关
				if (r == controlleActor || this == controlleActor) {
					if (hValue != 0 || (hValue == 0 && absorb)) {
						int imgId = ((hValue >= 0 ? 0 : 1) << 1) | (crit ? 0 : 1);
						int[] data = new int[7];
						data[GameScene.DAMAGE_NUM_IMGID] = imgId;
						data[GameScene.DAMAGE_NUM_X] = Tools.random(r.m_posX - m_scene.m_cameraX - 5, r.m_posX
								- m_scene.m_cameraX + 5);
						data[GameScene.DAMAGE_NUM_Y] = Tools.random(r.m_posY - m_scene.m_cameraY - DAMAGE_NUM_OFFSETY
								- 5,
								r.m_posY - m_scene.m_cameraY - DAMAGE_NUM_OFFSETY + 5);
						data[GameScene.DAMAGE_NUM_W] = GameScene.SIZE_IMG_DAMAGENUM[imgId][0];
						data[GameScene.DAMAGE_NUM_H] = GameScene.SIZE_IMG_DAMAGENUM[imgId][1];
						data[GameScene.DAMAGE_NUM_VALUE] = hValue;
						data[GameScene.DAMAGE_NUM_DURATION] = 10;
						scene.addDamageNum(data);
					}
				}

			}
			// 攻击未命中
			else {
				// 只有自己是施法方或者施法目标时才显示数值相关
				if (r == controlleActor || this == controlleActor) {
					int[] data = new int[7];
					data[GameScene.DAMAGE_NUM_IMGID] = GameScene.DAMAGE_NUM_IMGID_MISS;
					data[GameScene.DAMAGE_NUM_X] = Tools.random(r.m_posX - m_scene.m_cameraX - 5, r.m_posX
							- m_scene.m_cameraX + 5);
					data[GameScene.DAMAGE_NUM_Y] = Tools.random(r.m_posY - m_scene.m_cameraY - DAMAGE_NUM_OFFSETY - 5,
							r.m_posY - m_scene.m_cameraY - DAMAGE_NUM_OFFSETY + 5);
					// 未命中不需要知道图片宽高
					data[GameScene.DAMAGE_NUM_W] = GameScene.SIZE_IMG_DAMAGENUM[GameScene.DAMAGE_NUM_IMGID_MISS][0];
					data[GameScene.DAMAGE_NUM_H] = GameScene.SIZE_IMG_DAMAGENUM[GameScene.DAMAGE_NUM_IMGID_MISS][1];
					data[GameScene.DAMAGE_NUM_VALUE] = hValue;
					data[GameScene.DAMAGE_NUM_DURATION] = 10;
					scene.addDamageNum(data);
				}
			}
		}
		m_targetDamage.removeAllElements();
		return true;
	}

	/**
	 * 血量变化
	 * 
	 * @param hp
	 */
	public void hpChanged(int hp)
	{
		m_hp += hp;
		m_hp = Tools.limit(m_hp, 0, m_hpMax);
	}

	/**
	 * mp变化
	 * 
	 * @param mp
	 */
	public void mpChanged(int mp)
	{
		m_mp += mp;
		m_mp = Tools.limit(m_mp, 0, m_mpMax);
	}

	/**
	 * 当前动作是否可以被技能打断
	 * 
	 * @return
	 */
	public abstract boolean canBeInterrupt(boolean force);

	/**
	 * 死亡后的处理
	 */
	public void dead()
	{
		m_targetActor = null;
	}

	// ///////////////////////////////////////////////////////////
	// // 模拟行走
	// public short[][] m_flexBuffer = null;//拐点信息缓冲
	public ArrayList m_flexBuffer = new ArrayList(5, 5);
	public static final short FLEX_INDEX_X = 0;// 拐点x
	public static final short FLEX_INDEX_Y = 1;// 拐点y
	public static final short FLEX_INDEX_COUNT = 2;// 总数

	public void addFlex(short[] flex)
	{
		synchronized (m_flexBuffer) {
			if (m_flexBuffer.size() > 0) {
				short[] point = (short[]) m_flexBuffer.elementAt(m_flexBuffer.size() - 1);
				if (point[FLEX_INDEX_X] == flex[FLEX_INDEX_X] && point[FLEX_INDEX_Y] == flex[FLEX_INDEX_Y]) {
					return;
				}
			}
			m_flexBuffer.addElement(flex);
		}
	}

	/**
	 * 同屏更新的移动效果
	 */
	public void updateMove()
	{
		synchronized (m_flexBuffer) {
			if (m_flexBuffer != null && m_flexBuffer.size() > 0) {
				int size = m_flexBuffer.size();
				if( size > 3 ){
					short[] p = (short[])m_flexBuffer.elementAt((size-1));
					m_posX = p[FLEX_INDEX_X];
					m_posY = p[FLEX_INDEX_Y];
					for(int i=0;i<(size-1);i++){
						m_flexBuffer.removeElementAt(0);
					}
					return;
				}
				short[] point = (short[]) m_flexBuffer.elementAt(0);
				int dx = m_posX - point[FLEX_INDEX_X];
				int dy = m_posY - point[FLEX_INDEX_Y];
				if (dx != 0) {
					m_flipX = false;
					if (dx > 0) {
						move(DIRECT_W, Math.max(-m_velocity, -dx << Actor.ACTOR_FRACTION_BIT));
					}
					else {
						move(DIRECT_E, Math.min(m_velocity, -dx << Actor.ACTOR_FRACTION_BIT));
					}
				}
				else if (dy != 0) {
					if (m_direct == DIRECT_W) {
						m_flipX = true;
					}
					if (dy > 0) {
						move(DIRECT_N, Math.max(-m_velocity, -dy << Actor.ACTOR_FRACTION_BIT));
					}
					else {
						move(DIRECT_S, Math.min(m_velocity, -dy << Actor.ACTOR_FRACTION_BIT));
					}
				}
				
				if (Math.abs(dx << Actor.ACTOR_FRACTION_BIT) <= m_velocity && Math.abs(dy << Actor.ACTOR_FRACTION_BIT) <= m_velocity) {
					m_flexBuffer.removeElementAt(0);
				}
//				else {
//					stay(m_direct);
//					m_flexBuffer.removeElementAt(0);
//				}
			}
			else {
				stay(m_direct);
				// setAction(ACTION_STAND * DIRECT_NUM + m_direct, 0, 0, true,
				// false);
			}
		}
	}

	// public void updateMove()
	// {
	// synchronized (m_flexBuffer) {
	// if (m_flexBuffer != null && m_flexBuffer.size() > 0) {
	// short[] point = (short[]) m_flexBuffer.elementAt(0);
	// int dx = m_posX - point[FLEX_INDEX_X];
	// int dy = m_posY - point[FLEX_INDEX_Y];
	// if (dx != 0) {
	// m_flipX = false;
	// if (dx > 0) {
	// setAction(ACTION_MOVE * DIRECT_NUM + DIRECT_W,
	// Math.max(-m_velocity,
	// -dx << Actor.ACTOR_FRACTION_BIT), 0,
	// true, false);
	// }
	// else {
	// setAction(ACTION_MOVE * DIRECT_NUM + DIRECT_E,
	// Math.min(m_velocity,
	// -dx << Actor.ACTOR_FRACTION_BIT), 0,
	// true, false);
	// }
	// }
	// else if (dy != 0) {
	// if (m_direct == DIRECT_W) {
	// m_flipX = true;
	// }
	// if (dy > 0) {
	// setAction(ACTION_MOVE * DIRECT_NUM + DIRECT_N, 0,
	// Math.max(-m_velocity,
	// -dy << Actor.ACTOR_FRACTION_BIT), true,
	// false);
	// }
	// else {
	// setAction(ACTION_MOVE * DIRECT_NUM + DIRECT_S, 0,
	// Math.min(m_velocity,
	// -dy << Actor.ACTOR_FRACTION_BIT), true,
	// false);
	// }
	// }
	// else {
	// setAction(ACTION_STAND * DIRECT_NUM + m_direct, 0, 0, true,
	// false);
	// m_flexBuffer.removeElementAt(0);
	// }
	// }
	// else {
	// // setAction(ACTION_STAND * DIRECT_NUM + m_direct, 0, 0, true,
	// // false);
	// }
	// }
	// }

	// //模拟行走
	// ///////////////////////////////////////////////////////////

	public void draw(Graphics g, int camX, int camY)
	{
		if (m_scene != null) {
			if (this == ((GameScene) m_scene).m_controlledActor.m_targetActor) {
				GameScene.s_targetFrame.m_posX = m_posX;
				GameScene.s_targetFrame.m_posY = m_posY;
				GameScene.s_targetFrame.draw(g, camX, camY);
			}
		}
		super.draw(g, camX, camY);
	}

	public void dr(Graphics g)
	{
		super.draw(g, 0, 0);
	}

	public void dispose()
	{
		super.dispose();
		m_name = null;
		if (m_pathPoint != null) {
			m_pathPoint.removeAllElements();
			m_pathPoint = null;
		}
		if (m_targetDamage != null) {
			m_targetDamage.removeAllElements();
			m_targetDamage = null;
		}
		if (m_flexBuffer != null) {
			m_flexBuffer.removeAllElements();
			m_flexBuffer = null;
		}
		if (m_buf != null) {
			m_buf.removeAllElements();
			m_buf = null;
		}
		if (m_debuf != null) {
			m_debuf.removeAllElements();
			m_debuf = null;
		}
		m_targetActor = null;
		m_castSrcEffect = null;
		m_castTgtEffect = null;
	}

	public void destroy()
	{
		super.destroy();
		m_name = null;
		if (m_pathPoint != null) {
			m_pathPoint.removeAllElements();
			m_pathPoint = null;
		}
		if (m_targetDamage != null) {
			m_targetDamage.removeAllElements();
			m_targetDamage = null;
		}
		if (m_flexBuffer != null) {
			m_flexBuffer.removeAllElements();
			m_flexBuffer = null;
		}
		if (m_buf != null) {
			m_buf.removeAllElements();
			m_buf = null;
		}
		if (m_debuf != null) {
			m_debuf.removeAllElements();
			m_debuf = null;
		}
		m_targetActor = null;
		m_castSrcEffect = null;
		m_castTgtEffect = null;
	}

	// ////////////////////////////////////////////////////////////
	// // buf相关
	public static final int BUF_MAX_NUM = 6; // buf或者debf的最大数目
	public static final int NO_BUF_FLAG = -1; // 无buf的标志
	public Vector m_buf = new Vector(); // buf
	public Vector m_debuf = new Vector(); // debuf

	// public long m_curBufFlag;//角色当前buf状态
	
	public void clearBufAndDebuf() {
		if (m_buf != null) {
			m_buf.removeAllElements();
		}
		if (m_debuf != null) {
			m_debuf.removeAllElements();
		}
	}

	/**
	 * 查找buf和debuf
	 * @param id
	 */
	public ActorBuffer getBufByID(long id)
	{
		ActorBuffer buf;
		for (int i = 0; i < m_buf.size(); i++) {
			buf = (ActorBuffer) m_buf.elementAt(i);
			if (buf.m_guid == id) {
				return buf;
			}
		}
		for (int i = 0; i < m_debuf.size(); i++) {
			buf = (ActorBuffer)m_debuf.elementAt(i);
			if (buf.m_guid == id) {
				return buf;
			}
		}
		return null;
	}

	/**
	 * 添加buf
	 * 
	 * @param id
	 * @param duration 持续时间
	 * @param damageOverall 总治疗
	 * @param damageFrequency 治疗次数
	 * @param damageInterval 治疗间隔
	 */
	public void addBuf(long id, short iconId, int duration)
	{
		// m_curBufFlag |= (1 << id);
		ActorBuffer buf;
		for (int i = 0; i < m_buf.size(); i++) {
			buf = (ActorBuffer) m_buf.elementAt(i);
			if (buf.m_guid == id) {
				buf.init(this, ActorBuffer.TYPE_BUF, id, iconId, duration);
				return;
			}
		}
		if (m_buf.size() < BUF_MAX_NUM) {
			buf = new ActorBuffer();
			buf.init(this, ActorBuffer.TYPE_BUF, id, iconId, duration);
			m_buf.addElement(buf);
		}
	}

	/**
	 * 移除buf
	 * 
	 * @param id
	 */
	public void removeBuf(long id)
	{
		// m_curBufFlag &= ~(1 << id);
		ActorBuffer buf;
		for (int i = 0; i < m_buf.size(); i++) {
			buf = (ActorBuffer) m_buf.elementAt(i);
			if (buf.m_guid == id) {
				m_buf.removeElementAt(i);
				return;
			}
		}
	}

	/**
	 * 添加debuf
	 * 
	 * @param id
	 * @param duration 持续时间
	 * @param damageOverall 总伤害
	 * @param damageFrequency 伤害次数
	 * @param damageInterval 伤害间隔
	 */
	public void addDebuf(long id, short iconId, int duration)
	{
		// m_curBufFlag |= (1 << id);

		ActorBuffer buf;
		for (int i = 0; i < m_debuf.size(); i++) {
			buf = (ActorBuffer) m_debuf.elementAt(i);
			if (buf.m_guid == id) {
				buf.init(this, ActorBuffer.TYPE_DEBUF, id, iconId, duration);
				return;
			}
		}
		if (m_debuf.size() < BUF_MAX_NUM) {
			buf = new ActorBuffer();
			buf.init(this, ActorBuffer.TYPE_DEBUF, id, iconId, duration);
			m_debuf.addElement(buf);
		}
	}

	/**
	 * 移除debuf
	 * 
	 * @param id
	 */
	public void removeDeBuf(long id)
	{
		// m_curBufFlag &= ~(1 << id);
		ActorBuffer buf;
		for (int i = 0; i < m_debuf.size(); i++) {
			buf = (ActorBuffer) m_debuf.elementAt(i);
			if (buf.m_guid == id) {
				m_debuf.removeElementAt(i);
				return;
			}
		}
	}

	/**
	 * 比较buf(返回是否有指定buf)
	 * 
	 * @param state
	 * @return 有true，无false
	 */
	public boolean containBuffer(long bufId)
	{
		if (bufId == 0 || bufId == -1)
			return true;
		int size = m_buf.size();
		ActorBuffer buf;
		for (int i = 0; i < size; i++) {
			buf = (ActorBuffer) m_buf.elementAt(i);
			if (buf.m_guid == bufId)
				return true;
		}
		size = m_debuf.size();
		for (int i = 0; i < size; i++) {
			buf = (ActorBuffer) m_debuf.elementAt(i);
			if (buf.m_guid == bufId)
				return true;
		}
		return false;
	}

	// // buf相关
	// ////////////////////////////////////////////////////////////

	// ////////////////////////////////////////////////////////////
	// // 快捷栏相关

	public static final int ACTION_TYPE_NONE = 0;
	public static final int ACTION_TYPE_SKILL = 1;
	public static final int ACTION_TYPE_ITEM = 2;
	public static final int ACTION_TYPE_FUNCTION = 3;

	/*
	 * 功能ID
	 */
	public static final int ACTION_FUNCTION_TASKLIST = 0;
	public static final int ACTION_FUNCTION_MAP = 1;
	public static final int ACTION_FUNCTION_NPCLIST = 2;
	public static final int ACTION_FUNCTION_MOVE_E = 3;
	public static final int ACTION_FUNCTION_MOVE_S = 4;
	public static final int ACTION_FUNCTION_MOVE_W = 5;
	public static final int ACTION_FUNCTION_MOVE_N = 6;
	public static final int ACTION_FUNCTION_FINDPATH = 7;
	public static final int ACTION_FUNCTION_ROLEINFO = 8;
	public static final int ACTION_FUNCTION_SNS = 9;
	public static final int ACTION_FUNCTION_ROLEBAG = 10;
	public static final int ACTION_FUNCTION_TEAM = 11;
	public static final int ACTION_FUNCTION_MAIL = 12;
	public static final int ACTION_FUNCTION_AUTOFIGHTING = 13;
	public static final int ACTION_FUNCTION_SYSSETTING = 14;
	public static final int ACTION_FUNCTION_PET = 15;
	public static final int ACTION_FUNCTION_SKILL = 16;
	public static final int ACTION_FUNCTION_MACRO1 = 17;
	public static final int ACTION_FUNCTION_MACRO2 = 18;
	public static final int ACTION_FUNCTION_MACRO3 = 19;
	public static final int ACTION_FUNCTION_MACRO4 = 20;

	/*
	 * 功能对象：由于需要用Object对象，因此将功能ID包装成Integer进行处理
	 */
	public static final Integer[] ACTION_FUNCTIONS = {
			new Integer(ACTION_FUNCTION_TASKLIST),
			new Integer(ACTION_FUNCTION_MAP),
			new Integer(ACTION_FUNCTION_NPCLIST),
			new Integer(ACTION_FUNCTION_MOVE_E),
			new Integer(ACTION_FUNCTION_MOVE_S),
			new Integer(ACTION_FUNCTION_MOVE_W),
			new Integer(ACTION_FUNCTION_MOVE_N),
			new Integer(ACTION_FUNCTION_FINDPATH),
			new Integer(ACTION_FUNCTION_ROLEINFO),
			new Integer(ACTION_FUNCTION_SNS),
			new Integer(ACTION_FUNCTION_ROLEBAG),
			new Integer(ACTION_FUNCTION_TEAM),
			new Integer(ACTION_FUNCTION_MAIL),
			new Integer(ACTION_FUNCTION_AUTOFIGHTING),
			new Integer(ACTION_FUNCTION_SYSSETTING),
			new Integer(ACTION_FUNCTION_PET),
			new Integer(ACTION_FUNCTION_SKILL),
			new Integer(ACTION_FUNCTION_MACRO1),
			new Integer(ACTION_FUNCTION_MACRO2),
			new Integer(ACTION_FUNCTION_MACRO3),
			new Integer(ACTION_FUNCTION_MACRO4), };

	/*
	 * 功能图标ID
	 */
	public static final byte[] ACTION_FUNCTION_ICON = {

	};

	/**
	 * 快捷栏:2组，每组8个
	 */
	public long[][][] m_shortcut;

	/**
	 * 当前快捷栏索引
	 */
	public int m_shortcut_bar = 0;

	/**
	 * 根据流初始化技能栏
	 * 
	 * @param bar
	 * @param dis
	 */
	public void initShortcut(DataInputStream dis) throws IOException
	{
		int num = dis.readInt();
		long[][][] shortcut = new long[2][num / 2][3];
		for (int i = 0; i < num; i++) {
			int rowIndex = i / 8;
			int colIndex = i % 8;
			shortcut[rowIndex][colIndex][0] = dis.readInt();
			if (shortcut[rowIndex][colIndex][0] == ACTION_TYPE_NONE) {
				continue;
			}
			shortcut[rowIndex][colIndex][1] = dis.readLong();
//			Tools.debugPrintln("<<<<>>>>>shortcut["+rowIndex+"]["+colIndex+"][0]"+shortcut[rowIndex][colIndex][0]);
//			Tools.debugPrintln("<<<<>>>>>shortcut["+rowIndex+"]["+colIndex+"][1]"+shortcut[rowIndex][colIndex][1]);
		}
		m_shortcut = shortcut;
	}

	/**
	 * 获得快捷栏所对应的对象
	 * 
	 * @param bar
	 * @param inx
	 * @return
	 */
	public Object getShortcutData(int bar, int inx)
	{
		if (m_shortcut[bar][inx] == null) {
			return null;
		}
		long[] data = m_shortcut[bar][inx];
		int type = (int) data[0];
		long id = data[1];
		return getDataByTypeAndId(type, id);
	}

	public Object getDataByTypeAndId(int type, long id)
	{
		switch (type) {
			case ACTION_TYPE_SKILL: {
				RoleCombatSkill skill = GameManager.getInstance().m_role.getCombatSkill((short) id);
				return skill;
			}
			case ACTION_TYPE_ITEM: {
				ActorItem item = GameManager.getInstance().m_role.getActorItemByGuid(ActorItem.TYPE_BAG_ROLE, id) ;
				return item;
			}
			case ACTION_TYPE_FUNCTION: {
				MenuFunction menuFunction = new MenuFunction(id);
				return menuFunction;
			}
		}
		return null;
	}

	/**
	 * 修改快捷栏
	 * 
	 * @param type
	 * @param oldId
	 * @param newId
	 */
	public void resetShortcut(int type, long oldId, long newId)
	{
		if (m_shortcut == null) {
			return;
		}
		if (type == ACTION_TYPE_NONE) {
			throw new IllegalArgumentException("当前设置快捷键错误");
		}
		for (int i = 0; i < m_shortcut.length; i++) {
			for (int j = 0; j < m_shortcut[i].length; j++) {
				if (m_shortcut[i][j][0] == type && m_shortcut[i][j][1] == oldId) {
					m_shortcut[i][j][1] = newId;
				}
			}
		}
	}

	public void cleanShortcut(int type, long oldId)
	{
		if (m_shortcut == null) {
			return;
		}
		for (int i = 0; i < m_shortcut.length; i++) {
			for (int j = 0; j < m_shortcut[i].length; j++) {
				if (m_shortcut[i][j][0] == type && m_shortcut[i][j][1] == oldId) {
					m_shortcut[i][j][0] = AbstractGameActor.ACTION_TYPE_NONE;
					m_shortcut[i][j][1] = 0;
				}
			}
		}
	}

	/**
	 * 将快捷栏相应的技能全部删除
	 * 
	 * @param type
	 */
	public void clearShortCutSkillOnSkillChanged()
	{
		if (m_shortcut == null) {// 初始化技能的时候，快捷栏还未生成对象
			return;
		}
		for (int i = 0; i < m_shortcut.length; i++) {
			if (m_shortcut[i] == null) {
				continue ;
			}
			for (int j = 0; j < m_shortcut[i].length; j++) {
				if (m_shortcut[i][j] == null) {
					continue ;
				}
				if (m_shortcut[i][j][0] == ACTION_TYPE_SKILL) {
					RoleCombatSkill skill = (RoleCombatSkill) getDataByTypeAndId((byte) m_shortcut[i][j][0],m_shortcut[i][j][1]);
					if (skill == null || skill.m_level <= 0) {
						m_shortcut[i][j][0] = ACTION_TYPE_NONE;
						m_shortcut[i][j][1] = 0;
					}
				}
			}
		}
	}

	/**
	 * 根据data进行相应的动作
	 * 
	 * @param data
	 */
	public abstract int doAction(Object data);

	public void doFunction(int id)
	{
		switch (id) {
			case ACTION_FUNCTION_MACRO1:
			case ACTION_FUNCTION_MACRO2:
			case ACTION_FUNCTION_MACRO3:
			case ACTION_FUNCTION_MACRO4:
				break;
		}
	}

	// 切换
	private void doActionSwitchShotCut()
	{
		if (m_shortcut == null) {
			return;
		}
		if (m_shortcut_bar == m_shortcut.length - 1) {
			m_shortcut_bar = 0;
		}
		else {
			m_shortcut_bar++;
		}

		Tools.debugPrintln("doActionSwitchShotCut shortCutIndex = " + m_shortcut_bar);
		UIMain.updateShortcut();
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "切换到快捷栏" + (m_shortcut_bar + 1));
	}

	/**
	 * 快捷栏按键逻辑
	 */
	public void keyShortcut()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY1)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][0]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY2)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][1]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY3)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][2]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY4)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][3]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY5)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][4]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY6)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][5]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY7)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][6]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY8)) {
			GameManager.getInstance().m_role.stopMacro();
			doAction(m_shortcut[m_shortcut_bar][7]);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY9)) {
			GameManager.getInstance().m_role.stopMacro();
			doActionSwitchShotCut();
		}
	}

	// // 快捷栏相关
	// ////////////////////////////////////////////////////////////

}
